////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
//  All Rights Reserved. The following is Source Code and is subject to all
//  restrictions on such code as contained in the End User License Agreement
//  accompanying this product.
//
////////////////////////////////////////////////////////////////////////////////

package mx.controls.menuClasses
{

import flash.display.DisplayObject;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.IFlexDisplayObject;
import mx.core.UIComponent;
import mx.core.UITextField;
import mx.core.mx_internal;
import mx.styles.CSSStyleDeclaration;
import mx.styles.ISimpleStyleClient;

use namespace mx_internal;

include "../../styles/metadata/LeadingStyle.as"
include "../../styles/metadata/TextStyles.as"

/**
 *  The MenuBarItem class defines an item displayed in a MenuBar control.
 *  A MenuBarItem instance displays the text for the item and passes mouse 
 *  and keyboard interactions to the MenuBar so that it can correctly 
 *  show and hide menus. In general, this class is not explictly used. 
 *  Most customization are done to the Menu instances contained in the 
 *  MenuBar or to the MenuBar itself. 
 *
 *  @see mx.controls.Menu
 *  @see mx.controls.MenuBar
 */
public class MenuBarItem extends UIComponent implements IListItemRenderer
{
	include "../../core/Version.as";

	//--------------------------------------------------------------------------
	//
	//  Class constants
	//
	//--------------------------------------------------------------------------

	/**
	 *  @private
	 */
	private static const ITEM_WIDTH_BUFFER:Number = 20;

	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------

	/**
	 *  Constructor
	 */
	public function MenuBarItem()
	{
		super();
	}

	//--------------------------------------------------------------------------
	//
	//  Variables
	//
	//--------------------------------------------------------------------------

	/**
	 *  The UITextField that contains the text to display for this MenuBar item. 
	 */
	private var item:UITextField;

	/**
	 *  The skin defining the border and background for this MenuBar item. 
	 */
	private var currentSkin:IFlexDisplayObject;

	/**
	 *  @private
	 *  The text string that is displayed in the text field
	 */
	mx_internal var itemText:String;

	//--------------------------------------------------------------------------
	//
	//  Overridden properties: UIComponent
	//
	//--------------------------------------------------------------------------

	//----------------------------------
	//  enabled
	//----------------------------------

	/**
	 *  @private
	 */
	private var _enabled:Boolean = true;

	/**
	 *  @private
	 */
	private var enabledChanged:Boolean = false;

	/**
	 *  @private
	 */
	override public function set enabled(value:Boolean):void
	{
		_enabled = value;
		super.enabled = value;
		enabledChanged = true;

		invalidateProperties();
	}

	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------

	//----------------------------------
	//  data
	//----------------------------------

	/**
     *  The <code>data</code> property of the IListItemRenderer interface has no 
	 *  purpose in the MenuBarItem class. 
	 *  The property always has a value of <code>null</code>, 
	 *  and setting it has no effect. 
	 * 
	 *  @default null 
	 */
	public function get data():Object
	{
		return null;
	}

	/**
	 *  @private
	 */
	public function set data(value:Object):void
	{
	}

	//----------------------------------
	//  dataProvider
	//----------------------------------

	/**
	 *  @private
	 *  Storage for data provider
	 */
	private var _dataProvider:Object;
	
	/**
	 *  The object that provides the data for the Menu that is popped up
	 *  when this MenuBarItem is selected.
	 * 
	 *  @default "undefined"
	 */
	public function get dataProvider():Object
	{
		return _dataProvider
	}

	/**
	 *  @private
	 */
	public function set dataProvider(value:Object):void
	{
		_dataProvider = value;

		invalidateProperties();
	}
	
	//----------------------------------
	//  menuBarIndex
	//----------------------------------

	/**
	 *  The index of this MenuBarItem relative to other MenuBarItems
	 *  in the MenuBar. The index of the first, ie: left most, MenuBarItem
	 *  is 0 and increases as you move to the right. 
	 * 
	 *  @default "undefined"
	 */
	public var menuBarIndex:int;

	//--------------------------------------------------------------------------
	//
	//  Overridden methods: UIComponent
	//
	//--------------------------------------------------------------------------

	/**
	 *  @private
	 */
	override protected function createChildren():void
	{
        super.createChildren();

        var styleDeclaration:CSSStyleDeclaration = new CSSStyleDeclaration();
        styleDeclaration.factory = function():void
		{
			this.borderStyle = "none"
		};

        /*
        if (!currentBorder)
		{
			currentBorder = new ActivatorSkin();
			currentBorder.styleName = this;
			currentBorder.styleDeclaration = styleDeclaration;
			addChild(currentBorder);
		}
        */

		if (!item)
		{
			item = new UITextField();
			item.text = itemText;
			item.selectable = false;
			item.styleName = this;
			addChild(item);
		}

		invalidateSize();
		invalidateDisplayList();

		viewSkin("itemUpSkin");
	}

	/**
	 *  @private
	 */
	override protected function commitProperties():void
	{
		super.commitProperties();

		if (enabledChanged)
		{
			enabledChanged = false;
			if (item)
				item.enabled = _enabled;

			if (!_enabled)
				viewSkin("itemUpSkin");
		}
	}

	/**
	 *  @private
	 */
	override protected function measure():void
	{
		super.measure();

		measuredWidth = item.getExplicitOrMeasuredWidth() + ITEM_WIDTH_BUFFER;
		measuredHeight = item.getExplicitOrMeasuredHeight();
	}

	/**
	 *  @private
	 */
	override protected function updateDisplayList(unscaledWidth:Number,
												  unscaledHeight:Number):void
	{
		super.updateDisplayList(unscaledWidth, unscaledHeight);

		currentSkin.setActualSize(unscaledWidth, unscaledHeight);
		
		item.setActualSize(unscaledWidth - ITEM_WIDTH_BUFFER,
						   getExplicitOrMeasuredHeight());

		item.move(ITEM_WIDTH_BUFFER / 2,
				  (unscaledHeight - getExplicitOrMeasuredHeight()) / 2);
	}

	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------

	/**
	 *  @private
	 */
	mx_internal function viewSkin(state:String):void
	{
		var newSkin:IFlexDisplayObject =
			IFlexDisplayObject(getChildByName(state));

		if (!newSkin)
		{
			var newSkinClass:Class = getStyle(state);

			if (newSkinClass)
			{
				newSkin = new newSkinClass();

				DisplayObject(newSkin).name = state;

				if (newSkin is ISimpleStyleClient)
					ISimpleStyleClient(newSkin).styleName = this;

				addChildAt(DisplayObject(newSkin), 0);
			}
		}

		newSkin.setActualSize(unscaledWidth, unscaledHeight);

		if (currentSkin)
			currentSkin.visible = false;

		if (newSkin)
			newSkin.visible = true;

		currentSkin = newSkin;
	}
}

}
